<?php
/**
 * Created by: i@fedax.cn
 * Date: 2018/7/16 22:39
 */

namespace app\admin\controller;

use app\admin\model\AgentRecharge;
use app\admin\model\FamilyRoom;
use app\admin\model\IntroduceUser;
use app\admin\model\Message;
use app\admin\model\User as CustomerModel;
use app\admin\model\NiceId;
use think\cache\driver\Redis;

class Customer extends Index
{
    /**
     * @return \think\response\View
     */
    public function index()
    {
        return view();
    }
    /**
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @title 真实用户
     */
    public function indexData()
    {
        $customerModel = new CustomerModel();
        $user_type = 1;  //真实用户
        $data = $customerModel->getDataList($user_type);
        return json($data);
    }
    public function newCustomerIndex()
    {

        return view();
    }

    public function newCustomerIndexData()
    {

        $customerModel = new CustomerModel();
        $data = $customerModel->getNewCustomerData();

        return json($data);
    }
    public function robotIndex()
    {
        return view();
    }

    /**
     * @return \think\response\Json
     * @user 帅帅吴
     * @date 2018/12/21
     * @time 10:18
     * @title 机器人列表
     */
    public function robotIndexData()
    {
        $customerModel = new CustomerModel();
        $user_type = 2;  //机器人
        $data = $customerModel->getDataList($user_type);
        return json($data);
    }
    //机器人添加
    public function robotAdd()
    {
        $customerModel = new CustomerModel();
        $res = $customerModel->robotAdd();
        return json($res);
    }

    //机器人所在房间列表
    public function getRobotRoom()
    {
        $customerModel = new CustomerModel();
        $res = $customerModel->getRobotRoom();
        return json($res);
    }
    //未进入房间的机器人列表（视图）
    public function notEnterRoomRobot()
    {
        $customerModel = new CustomerModel();
        $data = $customerModel->notEnterRoomRobot();
        return view('not_enter_room_robot')->assign('data',$data);
    }
    //拉机器人进入房间
    public function robotEnterRoom()
    {
        $customerModel = new CustomerModel();
        $res = $customerModel->robotEnterRoom();
        return $res;
    }
    //批量拉机器人进入房间
    public function robotAllEnterRoom()
    {
        $customerModel = new CustomerModel();
        $res = $customerModel->robotAllEnterRoom();
        return $res;
    }

    //含有机器人的房间
    public function haveRobotRoom()
    {
        $customerModel = new CustomerModel();
        $res = $customerModel->haveRobotRoom();
        return view('have_robot_room')->assign(['data'=>$res['data'],'room_id'=>$res['room_id'] ]);
    }

    /**
     * @user 帅帅吴
     * @date 2019-02-26
     * @time 14:50
     * @title 禁封设备
     */
    public function banUserDevice()
    {
        $customerModel = new CustomerModel();
        $res = $customerModel->banUserDevice();
        return $res;
    }
    public function ridUserDevice()
    {
        $customerModel = new CustomerModel();
        $res = $customerModel->ridUserDevice();
        return $res;
    }

    /**
     * @return \think\response\View
     * @user shuaishuai wu
     * @date 2018/12/11
     * @time 13:14
     * 禁用用户
     */
    public function userBan()
    {
        $customerModel = new CustomerModel();
        $forever = input('get.forever'); //表单传的值
        $day = input('get.day');  //表单传的值
        if(!empty($forever) || !empty($day)){
            $res = $customerModel->userBan();
            return $res;
        }
        $userId = input('get.user_id');
        $type =  input('get.type',0);
        return view()->assign(['userId'=>$userId,'type'=>$type]);
    }

    /**
     * @user shuaishuai wu
     * @date 2018/12/11
     * @time 17:23
     * @title 解封用户
     */
    public function userRid()
    {
        $customerModel = new CustomerModel();
        $res = $customerModel->userRid();
        return $res;
    }

    /**
     * @return $this|array
     * @user 帅帅吴
     * @date 2019-20-15
     * @title 修改用户密码（超管仅有的权限）
     */
    public function userResetPassword()
    {
        $user_id = input('get.user_id');

        if(input('get.password')){
            $customerModel = new CustomerModel();
            $password = input('get.password');
//            $str = "0123456789abcdefghijklmnopqrstuvwxyz";             //输出字符集
//            $salt = substr(str_shuffle($str), 0, 6);
            $salt = $customerModel->where('id','=',$user_id)->value('salt');
            $md5_password = md5(md5($password . $salt));
            // halt($md5_password.'--'.$user_id);
            $res = $customerModel->save(['password'=>$md5_password],['id'=>$user_id]);
            if($res) return ['code'=>100,'msg'=>'密码设置成功'];
            else return ['code'=>-100,'msg'=>'密码设置失败'];
        }

        return view()->assign('user_id',$user_id);
    }

    /**
     * @user shuaishuai wu
     * @date 2018/12/11
     * @time 18:01
     * @title  修改用户的u_id
     */
    public function userEdit()
    {
        $user_id = input('get.user_id');
        $u_id = input('get.u_id');
        $customerModel = new CustomerModel();
        $re = $customerModel->where('u_id','=',$u_id)->find();
        if($re) return ['code'=>-200,'msg'=>'该号被占用,请更换'];
        $newData = ['u_id'=>$u_id];
        $res= $customerModel->save($newData,['id'=>$user_id]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else return ['code'=>-100,'msg'=>'修改失败'];
    }

    //重置原始ID
    public function resetUserId()
    {
        $customerModel = new CustomerModel();
        $user_id = input('get.user_id');

        $userInfo = $customerModel->where('id','eq',$user_id)->find();
        if(empty($userInfo['old_uid'])) return ['code'=>-100,'msg'=>'原始ID不存在'];

        $ree =  $customerModel->where('u_id','eq',$userInfo['old_uid'])->find();
        if($ree) return ['code'=>-100,'msg'=>'原始ID已被占用'];

        $re = NiceId::where('nice_num','eq',$userInfo['u_id'])->find();
        if(empty($re))  return ['code'=>-100,'msg'=>'目前ID不属于靓号，不能重置原始ID'];

        $res = $customerModel->save(['u_id'=>$userInfo['old_uid']],['id'=>$user_id]);
        $ress = NiceId::where('nice_num','eq',$userInfo['u_id'])->update(['status'=>1]);
        if($res && $ress) return ['code'=>100,'msg'=>'重置成功'];
        else return ['code'=>-100,'msg'=>'重置失败'];
    }

    /**
     * @user shuaishuai wu
     * @date 2018/12/11
     * @time 18:50
     * @title 开通家族
     */
    public function userBuildFamily()
    {
        $familyModel = new \app\admin\model\Family();
        $res = $familyModel->createFamily();
        if($res) return ['code'=>100,'msg'=>'创建成功'];
        else return ['code'=>-100,'msg'=>'创建失败'];
    }

    /**
     * @return \think\response\View
     * @user 帅帅wu
     * @date 2018/12/12
     * @time 13:53
     * @title 创建预审家族
     */
    public function userBuildAgreeFamily()
    {
        $userId = input('get.user_id');
        return view()->assign('userId',$userId);
    }

    public function userBuildAgreeFamilyInfo()
    {
        $familyModel = new \app\admin\model\Family();
        $res = $familyModel->userBuildAgreeFamilyInfo();
        return $res;
    }

    //推荐人列表
    public function introduceUserIndex()
    {
        return view();
    }

    /**
     * @return \think\response\Json
     * @user shuaisuhai wu
     * @date 2018/12/7
     * @time 13:20
     * @title 邀请人列表数据
     */
    public function introduceUserIndexData()
    {

        $introduceUserModel = new IntroduceUser();
        $data = $introduceUserModel->getUserInfo();
        return json($data);

    }

    /**
     * @return $this
     * @user 帅帅wu
     * @date 2018/12/07
     * @time 13:21
     * @title 邀请人成员
     */
    public function introduceMember()
    {
        $introduceUserModel = new IntroduceUser();
        $data = $introduceUserModel->introduceMember();

        return view()->assign('data',$data);
    }


    public function introduceAllMember()
    {
        $introduceUserModel = new IntroduceUser();
        $data = $introduceUserModel->introduceAllMember();

        return ['resultCode'=>100,'resultData'=>$data];
    }


    public function ridUserLock()
    {
        $user_id = input('user_id');
        $redis = new Redis();
//        if($redis->get('user_sendGift_lock:'.$user_id))
        $re = $redis->rm('user_sendGift_lock:'.$user_id);
//        if($redis->get('user_redpacket_lock:'.$user_id))
        $res = $redis->rm('user_redpacket_lock:'.$user_id);
//        if($redis->get('user_lock:'.$user_id))
        $ree = $redis->rm('user_lock:'.$user_id);
        if($re || $res || $ree)
            return ['code'=>100,'msg'=>'成功'];
        else
            return ['code'=>-100,'msg'=>'该用户未被禁封宝箱'];

    }

    public function diamondsSource()
    {
        $user_id = input('user_id');
        return view()->assign('user_id',$user_id);
    }

    public function diamondsSourceData()
    {
        $user_id = input('user_id');
        $start_time  = strtotime(input('start_time'));
        $end_time  = strtotime(input('end_time'));
        if($start_time && $end_time) $map[] = ['create_time','between',[$start_time,$end_time] ];
        elseif ($start_time)   $map[] = ['create_time','egt',$start_time ];
        else $map[] = ['create_time','elt',$end_time ];
        $map[] = ['user_id','eq',$user_id];
        //手机充值
        $w_recharge = \app\admin\model\Recharge::where($map)->where('status','eq',2)->sum('diamonds');
        //官方充值
        $a_recharge = \app\admin\model\GivePresen::where($map)->where('prize_type','eq',2)->sum('prize_number');
        //厅主充值
        $map[1] = ['receive_user_id','eq',$user_id];
        $h_recharge = AgentRecharge::where($map)->sum('recharge_diamonds');

        $t_recharge = $w_recharge + $a_recharge + $h_recharge;
        $result =  ['w_recharge'=>$w_recharge,'a_recharge'=>$a_recharge,'h_recharge'=>$h_recharge,'t_recharge'=>$t_recharge];
        return ['code'=>100,'data'=>$result];
    }


}